package jsp;

import entity.Emp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author 石小俊
 * @date 2023年10月24日 14:56
 */
@WebServlet("*.session")
public class SessionServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        String path = request.getServletPath();
        switch (path) {
            case "/add.session":
                addSession(request, response);
                break;
            case "/remove.session":
                removeSession(request, response);
                break;
            case "/clean.session":
                cleanSession(request, response);
                break;
            case "/modify.session":
                modifySession(request, response);
                break;
            case "/find.session":
                findSession(request, response);
                break;
            case "/setMaxAge.session":
                setMaxAge(request, response);
                break;
            default:
                PrintWriter out = response.getWriter();
                out.println("<h1 style='color:red;'>您输入的请求不存在,请检查请求是否正确!</h1>");
                break;
        }

    }

    protected void addSession(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取session对象
        HttpSession session = request.getSession(true);
//        HttpSession session2 = request.getSession(false);
        // 向session中存储数据时,其数据可以是任意类型
        session.setAttribute("msg", "Hello session");
        Emp emp = new Emp();
        emp.setId(1);
        emp.setName("admin");
        session.setAttribute("emp", emp);
    }

    protected void modifySession(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 修改的本质也是添加,当key相同时,会覆盖原有的数据
        request.getSession().setAttribute("msg", "Hello World");
    }

    protected void removeSession(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 删除指定key的数据
        request.getSession().removeAttribute("msg");
    }

    protected void cleanSession(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 清空整个session
        // 该方法的实现机制
        // 首先删除当前的session对象
        // 创建一个新的session对象
        request.getSession().invalidate();

    }

    protected void findSession(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        String msg = (String) session.getAttribute("msg");
        System.out.println("msg:"+msg);
        Emp emp = (Emp) session.getAttribute("emp");
        System.out.println(emp);
    }

    protected void setMaxAge(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置session的生存时间,默认情况下,session的生存时间是半小时
        // 可以通过方法修改其生存时间
        // 在实际开发中一般不进行修改
        request.getSession().setMaxInactiveInterval(2);
        // 注意点:此时设置为0并不是表示删除
        // 而是表示session永不过期
        request.getSession().setMaxInactiveInterval(0);
    }
}
